package org.eclipse.epsilon.emc.spreadsheets.csv.edit;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.eclipse.epsilon.common.util.FileUtil;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetPropertySetter;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvModel;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvWorksheet;
import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.junit.BeforeClass;
import org.junit.Test;

public class NoHeaderEditInstanceTest
{
	private static CsvModel model = null;
	private static String filePath = "resources/CsvFile.txt";

	@BeforeClass
	public static void setUpFileAndLoad() throws Exception
	{
		String firstLine = "v0,v1,v2,v3,v4,v5" + System.getProperty("line.separator");
		String secondLine = "v0,v1,v2,v3,v4,v5" + System.getProperty("line.separator");
		String contents = firstLine + secondLine;
		FileUtil.setFileContents(contents, new File(filePath));
		setUp();
	}

	private static void setUp() throws Exception
	{
		model = new CsvModel();
		model.setFile(filePath);
		model.setConfigurationFile("resources/CsvFileTestConfig.xml");
		model.setHasHeader(false);
		model.setStoredOnDisposal(false);
		model.setName("MODEL");
		model.load();
	}

	@Test
	public void editRow() throws EolRuntimeException
	{
		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 2);
		CsvRow row = (CsvRow) rows.get(0);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals("v0"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("v1"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("v2"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("v3"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("v4"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("v5"));

		SpreadsheetPropertySetter setter = new SpreadsheetPropertySetter(model);
		setter.invoke(row, row.getWorksheet().getColumn("col1"), "new_value1");

		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals("v0"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("new_value1"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("v2"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("v3"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("v4"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("v5"));
	}

	@Test
	public void editRowWithEOL() throws Exception
	{
		final EolModule module = new EolModule();
		module.parse("var row = MODEL!CSV.all.first(); row.col1 = 'new_value1';");
		module.getContext().getModelRepository().addModel(model);
		module.execute();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 2);
		CsvRow row = (CsvRow) rows.get(0);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals("v0"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("new_value1"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("v2"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("v3"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("v4"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("v5"));
	}

	@Test
	public void editAllRowsWithEOL() throws Exception
	{
		final EolModule module = new EolModule();
		module.parse("var row = MODEL!CSV.all; row.col1 = 'new_value1';");
		module.getContext().getModelRepository().addModel(model);
		module.execute();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 2);
		for (int i = 0; i < 2; i++)
		{
			CsvRow row = (CsvRow) rows.get(i);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals("v0"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("new_value1"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("v2"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("v3"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("v4"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("v5"));
		}
	}

}
